/*
 *     UserDaoImpl.java  Copyright (c) 2021. cc01cc. All rights reserved.
 *
 *     This file is part of e-commerce.
 *
 *     e-commerce is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     e-commerce is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package dao;

import org.junit.Test;
import pojo.User_register;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl  implements UserDao{
    @Override
    public User_register getLoginUser(Connection connection, String userName){
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User_register user = null;

//        System.out.println("UserDaoImpl: " + userName);
//        System.out.println(connection.toString());
        if(connection != null){
            String sql = "select * from e_commerce_db.user_register where user_name= ?";

            Object[] params = {userName};

            try{
//                System.out.println("进入UserDao，获取resultSet");
                resultSet = BasicDao.execute(connection, sql, params, preparedStatement, resultSet);

                if(resultSet.next()){
//                    System.out.println("user_name"+resultSet.getString("user_name"));
                    user = new User_register();
                    user.setUser_id(resultSet.getInt("user_id"));
                    user.setUser_name(resultSet.getString("user_name"));
                    user.setUser_pwd(resultSet.getString("user_pwd"));
                }
                BasicDao.closeResource(connection,preparedStatement,resultSet);
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        return user;
    }

    @Test
    public void testUserDao(){
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User_register user = null;
        Connection connection = BasicDao.getConnection();
        String userName = "u_test01";
//        System.out.println("UserDaoImpl: " + userName);

        if(connection != null){
            String sql = "select * from e_commerce_db.user_register where user_name= ?";

            Object[] params = {userName};

            try{
                resultSet = BasicDao.execute(connection, sql, params, preparedStatement, resultSet);

                if(resultSet.next()){
                    System.out.println("user_name"+resultSet.getString("user_name"));
                    user = new User_register();
                    user.setUser_id(resultSet.getInt("user_id"));
                    user.setUser_name(resultSet.getString("user_name"));
                    user.setUser_pwd(resultSet.getString("user_pwd"));
                }
                BasicDao.closeResource(connection,preparedStatement,resultSet);
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
}
